// 端口测试脚本
// 此脚本用于测试不同端口下API的可访问性

// 测试配置
const testPorts = [8000, 8005]; // 要测试的端口列表
const testEndpoints = [
  '/',
  '/api-info',
  '/test/document-count'
]; // 要测试的端点

// 运行测试
async function runTests() {
  console.log('开始端口测试...');
  const results = [];

  for (const port of testPorts) {
    console.log(`\n测试端口: ${port}`);
    const portResults = { port, endpoints: [] };

    for (const endpoint of testEndpoints) {
      const url = `http://localhost:${port}${endpoint}`;
      console.log(`测试端点: ${url}`);

      try {
        const response = await fetch(url, {
          method: 'GET',
          credentials: 'include' // 包含Cookie
        });

        const result = {
          endpoint,
          status: response.status,
          statusText: response.statusText,
          success: response.ok
        };

        // 尝试解析JSON响应
        try {
          result.data = await response.json();
        } catch (e) {
          result.data = null;
          result.parseError = '无法解析JSON响应';
        }

        portResults.endpoints.push(result);
        console.log(`结果: ${response.status} ${response.statusText}`);
      } catch (e) {
        portResults.endpoints.push({
          endpoint,
          error: e.message,
          success: false
        });
        console.log(`错误: ${e.message}`);
      }
    }

    results.push(portResults);
  }

  // 输出总结
  console.log('\n测试总结:');
  results.forEach(result => {
    console.log(`\n端口 ${result.port}:`);
    result.endpoints.forEach(endpoint => {
      if (endpoint.success) {
        console.log(`  ✓ ${endpoint.endpoint}: ${endpoint.status} ${endpoint.statusText}`);
      } else {
        console.log(`  ✗ ${endpoint.endpoint}: ${endpoint.error || `${endpoint.status} ${endpoint.statusText}`}`);
      }
    });
  });

  return results;
}

// 运行测试并处理结果
runTests()
  .then(results => {
    // 可以在这里添加结果处理逻辑
  })
  .catch(error => {
    console.error('测试失败:', error);
  });